package com.luoxiang.socialandroid.activity;

import static com.luoxiang.lib_common.common.Constants.MmkvConst.EXTRA;
import static com.luoxiang.lib_common.common.Constants.MmkvConst.KEY;
import static com.luoxiang.lib_common.common.Constants.MmkvConst.TOKEN;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.LogUtils;
import com.google.gson.Gson;
import com.luoxiang.socialandroid.R;
import com.luoxiang.socialandroid.adapter.InviteawardAdapter;
import com.luoxiang.socialandroid.base.BaseActivity;
import com.luoxiang.socialandroid.bean.InviteawardBean;
import com.luoxiang.socialandroid.bean.NumberBean;
import com.luoxiang.socialandroid.utils.AESUtil;
import com.luoxiang.socialandroid.utils.MmkvUtil;
import com.luoxiang.socialandroid.utils.NetUtils2;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class InviteawardActivity extends BaseActivity {
    private ImageView icon_back;
    private TextView tx_num;
    private String token;
    private RecyclerView rc_award;
    private InviteawardAdapter adapter;

    @Override
    protected void initListener() {
        icon_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @Override
    protected void initLayout(Bundle savedInstanceState) {
        setContentView(R.layout.activity_inviteaward);
        setStatusBarTranslucent();
    }

    @Override
    protected void initView() {
        showLoading();
        token = MmkvUtil.INSTANCE.decodeString(TOKEN);
        icon_back = findViewById(R.id.icon_back);
        tx_num = findViewById(R.id.tx_num);
        rc_award = findViewById(R.id.rc_award);
        Intent intent = getIntent();
        getRequest();
        double credit = intent.getDoubleExtra("credit", 0);
        tx_num.setText(String.valueOf(credit));
        getInviteAward(token, EXTRA);
    }

    private void getInviteAward(String token, String extra) {
        NetUtils2.getInstance().getApiService().getInviteAward(token, extra, 1, 10)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<NumberBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(NumberBean numberBean) {
                        clearLoading();
                        String data = numberBean.getData();
                        String decryptStr = null;
                        try {
                            decryptStr = AESUtil.aesDecryptString(data, KEY);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        try {
                            LogUtils.i("decryptStr0", decryptStr);
                            Gson gson = new Gson();
                            InviteawardBean bean = gson.fromJson(decryptStr, InviteawardBean.class);

                            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(InviteawardActivity.this);
                            rc_award.setLayoutManager(linearLayoutManager);
                            adapter = new InviteawardAdapter(bean.getData().getAwards());
                            rc_award.setAdapter(adapter);
                        } catch (Exception e) {
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        clearLoading();
                    }

                    @Override
                    public void onComplete() {
                        clearLoading();
                    }
                });
    }
}
